/**
 * \file
 *
 * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Subject to your compliance with these terms, you may use Microchip
 * software and any derivatives exclusively with Microchip products.
 * It is your responsibility to comply with third party license terms applicable
 * to your use of third party software (including open source software) that
 * may accompany Microchip software.
 *
 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
 */

#include "bootloader.h"

#ifdef MEDIA_USE_USART

static void _media_usart_pins_init(void)
{
  PMC->PMC_PCER0 = (1 << ID_PIOA);
  
  // 配置PA11为USART0_TXD功能
  PIOA->PIO_PDR = PIO_PDR_P11;      // 禁用引脚的输入输出功能（设置为外设功能）
  PIOA->PIO_ABSR &= ~PIO_ABSR_P11;  // 选择外设A功能（对应USART0_TXD）
  PIOA->PIO_PUER = PIO_PUDR_P11;    // 禁用上拉电阻（可根据实际情况决定是否需要上拉等）
  
  // 配置PA10为USART0_RXD功能
  PIOA->PIO_PDR = PIO_PDR_P10;
  PIOA->PIO_ABSR &= ~PIO_ABSR_P10;
  PIOA->PIO_PUER = PIO_PUER_P10;  // 根据需要可启用上拉电阻，防止信号干扰等，也可按需禁用
}


/**
 * Initialize USART serial as debug communication port
 */
void media_usart_init(void)
{
    /* USART pins init */
	_media_usart_pins_init();
	
	const sam_usart_opt_t options = {
		115200,
		US_MR_CHRL_8_BIT,
		US_MR_PAR_NO,
		US_MR_NBSTOP_1_BIT,
		US_MR_CHMODE_NORMAL,
		/* This field is only used in IrDA mode. */
		0
	};
	
	/* Enable the peripheral clock in the PMC. */
	sysclk_enable_peripheral_clock(ID_USART0);
    
	/* Configure USART in serial mode. */
	usart_init_rs232(MEDIA_USART_BASE, &options,sysclk_get_peripheral_hz());
	
	/* Enable the receiver and transmitter. */
    usart_enable_tx(MEDIA_USART_BASE);
    usart_enable_rx(MEDIA_USART_BASE);
}

/**
 * Close USART serial debug communication port
 */
void media_usart_cleanup(void)
{
	usart_disable_tx(MEDIA_USART_BASE);
	usart_disable_rx(MEDIA_USART_BASE);
	usart_reset(MEDIA_USART_BASE);
	pmc_disable_periph_clk(MEDIA_USART_ID);
}

/**
 * Check if there is console input
 * \return true if there is input
 */
uint8_t media_usart_rx_ready(void)
{
	return usart_serial_is_rx_ready(MEDIA_USART_BASE);
}

/**
 * Get a byte from console
 * \return byte read
 */
uint8_t media_usart_getchar(void)
{
	uint8_t byte;
	usart_serial_getchar(MEDIA_USART_BASE, &byte);
	return byte;
}

/**
 * Put a byte to console
 * \param c The byte to put
 */
void media_usart_putchar(char c)
{
	usart_serial_putchar(MEDIA_USART_BASE, c);
}


void media_usart_put_string(uint8_t *s)
{
	while (*s != '\0')
	{
		media_usart_putchar(*s);
		s++;
	}
}

/**
  * @brief  Test to see if a key has been pressed on the HyperTerminal
  * @param  key: The key pressed
  * @retval 1: Correct
  *         0: Error
  */
unsigned int media_usart_key_pressed(uint8_t *key)
{
  if(media_usart_rx_ready())
  {
     *key = (uint8_t)media_usart_getchar();
     return 1;
  }
  else
  {
    return 0;
  }
}
#endif /* #ifdef MEDIA_USE_USART */

